Skip to content

Fix #696: write non-finite TOML floats as nan/inf/-inf#697

Open
seonwooj0810 wants to merge 1 commit into
FasterXML:2.xfrom
seonwooj0810:fix/issue-696-toml-nonfinite-float
Open

Fix #696: write non-finite TOML floats as nan/inf/-inf#697
seonwooj0810 wants to merge 1 commit into
FasterXML:2.xfrom
seonwooj0810:fix/issue-696-toml-nonfinite-float

Conversation

@seonwooj0810

Copy link
Copy Markdown

Fixes #696

Root cause

TomlGenerator.writeNumber(double) and writeNumber(float) render the value with String.valueOf(...). For non-finite values that yields Java's textual forms NaN, Infinity, and -Infinity, which are not valid TOML floats. As a result the writer produces output it cannot read back — TomlMapper.readTree(...) on the writer's own output throws TomlStreamReadException: Unknown token.

Change

Map non-finite values to the TOML float tokens nan / inf / -inf (a small shared helper), leaving finite values formatted exactly as before. This is a writer-only change: the parser already accepts nan, inf, +inf, and -inf (Parser line ~389), so written non-finite values now round-trip with no reader change.

Tests

Added to TomlGeneratorTest:

  • nonFiniteDoubles / nonFiniteFloats — assert exact output abc = nan|inf|-inf
  • nonFiniteRoundTrip — writes NaN/±Infinity and reads them back through the same TomlMapper

All three fail before the change (two assertion failures plus a Unknown token error — the exact symptom in the issue) and pass after.

Verification done: ./mvnw -pl toml -am test -Dtest=TomlGeneratorTest — 23 tests pass with the fix; reverting only the TomlGenerator change reproduces 2 failures + 1 Unknown token error.

TomlGenerator wrote non-finite doubles/floats via String.valueOf(), emitting
Java's NaN/Infinity/-Infinity, which are not valid TOML and cannot be parsed
back by the reader (fails with 'Unknown token'). Emit the TOML float tokens
nan/inf/-inf instead so writer output round-trips.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant